import { getTime, parseTime } from '../../../../utils/timeUtil';
const app = getApp();

Page({
  /**
   * 页面的初始数据
   */
  data: {
    payType: [{
        name: "日结",
        id: 1,
        select: false
      },
      {
        name: "周结",
        id: 2,
        select: false
      },
      {
        name: "月结",
        id: 3,
        select: false
      }
    ],

    interviewTime: [
      {
        name: getTime(Date.now(),1) + '/' + getTime(new Date(),2) && '今天',
        timeStamp: parseTime(Date.now(), '{y}-{m}-{d}'),
        select:false
      },
      {
        name: getTime(Date.now()+86400000,1) + '/' + getTime(Date.now()+86400000,2),
        timeStamp: parseTime(Date.now()+86400000, '{y}-{m}-{d}'),
        select:false
      },
      {
        name: getTime(Date.now()+86400000*2,1) + '/' + getTime(Date.now()+86400000*2,2),
        timeStamp: parseTime(Date.now()+86400000*2, '{y}-{m}-{d}'),
        select:false
      },
      {
        name: getTime(Date.now()+86400000*3,1) + '/' + getTime(Date.now()+86400000*3,2),
        timeStamp: parseTime(Date.now()+86400000*3, '{y}-{m}-{d}'),
        select:false
      },
      {
        name: getTime(Date.now()+86400000*4,1) + '/' + getTime(Date.now()+86400000*4,2),
        timeStamp: parseTime(Date.now()+86400000*4, '{y}-{m}-{d}'),
        select:false
      },
      {
        name: getTime(Date.now()+86400000*5,1) + '/' + getTime(Date.now()+86400000*5,2),
        timeStamp: parseTime(Date.now()+86400000*5, '{y}-{m}-{d}'),
        select:false
      },
      {
        name: getTime(Date.now()+86400000*6,1) + '/' + getTime(Date.now()+86400000*6,2),
        timeStamp: parseTime(Date.now()+86400000*6, '{y}-{m}-{d}'),
        select:false
      },
      {
        name: getTime(Date.now()+86400000*7,1) + '/' + getTime(Date.now()+86400000*7,2),
        timeStamp: parseTime(Date.now()+86400000*7, '{y}-{m}-{d}'),
        select:false
      }
    ],

    from: '',

  },


  onLoad(option) {
    const from = option.from;
    const payTypeResult = JSON.parse(option.payTypeList);
    const interviewTimeResult = option.interviewTimeList && JSON.parse(option.interviewTimeList);
    if (payTypeResult) {
      this.data.payType.forEach((item) => {
        for(let i = 0;i<payTypeResult.length;i++){
          if(payTypeResult[i] == item.id){
            item.select = true;
            break;
          }
        }
      });
    }

    if(interviewTimeResult){
      this.data.interviewTime.forEach(item=>{
        for(let i= 0;i<interviewTimeResult.length;i++){
          if(interviewTimeResult[i] == item.timeStamp){
            item.select = true;
            break;
          }
        }
      });
    }

    this.setData({
      from: from,
      payType: this.data.payType,
      interviewTime: this.data.interviewTime
    })

  },

  selectPayType(e) {
    const index = e.currentTarget.dataset.index;
    this.data.payType[index].select = !this.data.payType[index].select;
    this.setData({
      payType: this.data.payType,
    });
  },

  selectInterviewTime(e) {
    const index = e.currentTarget.dataset.index;
    this.data.interviewTime[index].select = !this.data.interviewTime[index].select;
    this.setData({
      interviewTime: this.data.interviewTime,
    });
  },


  resetFilter() {
    this.data.payType.forEach(item => {
      item.select = false;
    });
    this.data.interviewTime.forEach(item => {
      item.select = false;
    });
    this.setData({
      payType: this.data.payType,
      interviewTime: this.data.interviewTime
    })
  },

  submit() {
    if (this.data.from == "homePage") {
      app.$routes.back(app.$routes.home);
    } else if(this.data.from == "searchPage"){
      app.$routes.back(app.$routes.search);
    }
  },

  onUnload(){
    const resultPayTypeResult = this.data.payType.filter(item => item.select)
      .map(item => item.id);
    const eventChannel = this.getOpenerEventChannel();
    if (this.data.from == "homePage") {
      eventChannel.emit("homeSelectOtherPage", {
        resultPayType: resultPayTypeResult,
      });
    } else if(this.data.from == "searchPage"){
      let interviewTimeList = this.data.interviewTime.filter((item)=>{
        return item.select;
      }).map(item=>{
        return item.timeStamp;
      });
      eventChannel.emit("searchPage", {
        resultPayType: resultPayTypeResult,
        resultInterviewTime: interviewTimeList
      })
    }
  }
})